package com.zoe.app.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Zoe.Zhang
 * @date 2021/10/10
 * @description
 */
@WebServlet(urlPatterns = "/hi")
public class DynamicServlet2 extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(DynamicServlet2.class);


    // 该URL第一次被访问的时候调用初始化方法,后续调用不会再触发该方法
    @Override
    public void init() throws ServletException {
        log.error("servlet2-init");
    }

    @Override
    // 每次访问该URL的Servlet都会触发该方法,处理请求
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        log.error("servlet2-service");
        super.service(req, res);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.error("servlet2-doGet");
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.error("servlet2-doPost");
        resp.getWriter().write("hi");
    }

    // web销毁的时候触发的方法 - 前提是该servlet已经被创建
    @Override
    public void destroy() {
        log.error("servlet2-destroy");
    }
}
